#ifndef CDATAMANAGER_H
#define CDATAMANAGER_H
#include <QString>
#include <QDateTime>
#include <QJsonObject>
#include <QSqlDatabase>
#include <QSqlError>
#include <string>
#include <memory>
#include <mutex>
#include <thread>

struct PositionData {
    int id;
    QString robotSn;
    QDateTime dateTime;
    QString position;
    int width;
    int height;
};

class  CDataManager {
public:
    ~CDataManager();
    CDataManager(const CDataManager &) = delete;
    CDataManager& operator=(const CDataManager&)=delete;
    static CDataManager& GetInstance();
    bool initDB();
    bool createPosTable(const QString& tableName);
    bool tableExists(const QString& tableName);
    QSqlDatabase dbConnection();
    int queryTotalNumber(const QString& sql);
private :
    CDataManager();
    void keepalive();
    void startHandlerProc(void);
private:
    QSqlDatabase m_db;
    static std::unique_ptr<CDataManager> m_InStance;
    static std::once_flag m_Flag;
    std::thread m_handler; //创建线程句柄
    std::string m_dbPath;  // 数据库名称
    std::string m_robotSn; // 设备Sn
    std::string m_position; // 位置表
    int id;
    QString robotSn;
    QDateTime dateTime;

    int width;
    int height;

};

#endif // CONFIG_H
